/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.dao.hibernate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.integrationpath.mengine.dao.ArticleDao;
import com.integrationpath.mengine.model.Article;
import com.integrationpath.mengine.model.Tag;


public class ArticleDaoHibernate extends GenericDaoHibernate<Article, Long> implements ArticleDao {

    public ArticleDaoHibernate() {
        super(Article.class);
    }

	public List<Article> getArticlesByCategoryName(String categoryName) {
		return getHibernateTemplate().find("from Article a where a.category.name=?", categoryName);
	}

	public List<Article> getArticlesByMediaType(int mediaId) {
		return getHibernateTemplate().find("select distinct m.article from Media m where m.mediaType.id=?", mediaId);
	}

	public List<Article> isArticleContainingPictures(Long articleId) {
		return getHibernateTemplate().find("select distinct m from Media m where m.mediaType.id=3 and m.article.id=?", articleId);
	}

	public List<Article> getArticlesByType(int typeId) {
		return getHibernateTemplate().find("from Article a where a.type.id=? and a.status=1", typeId);
	}

	public List<Article> getArticlesBySize(int size) {
		Criteria criteria = getSession().createCriteria(Article.class);
		criteria.setMaxResults(size);
		criteria.setCacheable(true);
		criteria.addOrder(Order.desc("date"));
		criteria.add(Expression.eq("status",new Byte("1")));
		return criteria.list();
	}

	public List<Article> getArticlesByVotings(int size) {
		Criteria criteria = getSession().createCriteria(Article.class);
		criteria.setMaxResults(size);
		criteria.setCacheable(true);
		criteria.add(Expression.eq("status",new Byte("1")));
		criteria.addOrder(Order.desc("ratingsNumber"));
		return criteria.list();
	}

	public List<Article> getArticlesByTypeAndSize(int typeId, int size) {
		Criteria criteria = getSession().createCriteria(Article.class);
		criteria.setMaxResults(size);
		criteria.setCacheable(true);
		criteria.add(Expression.eq("status",new Byte("1")));
		criteria.add(Expression.eq("type.id",typeId));
		criteria.addOrder(Order.desc("date"));
		return criteria.list();
	}

	public List<Article> getArticlesByUser(long userId) {
		return getHibernateTemplate().find("from Article a where a.appUser.id=?", userId);
	}

	public List getArticlesByTagName(String name) {
		return getHibernateTemplate().find("select t.article from Tag t where t.name=? and t.article.status=1", name);
	}

	public List<Article> getArticlesByDate(int year, int month) {
		Criteria criteria = getSession().createCriteria(Article.class);
		criteria.addOrder(Order.desc("date"));
		criteria.setCacheable(true);
		Calendar rightNow = Calendar.getInstance();
		Calendar variableCalendar = Calendar.getInstance();
		rightNow.set(Calendar.YEAR, year);
		rightNow.set(Calendar.MONTH, month - 1);
		
		List filteredeList = new ArrayList();
		
		List articles = criteria.list();
		for (Object object : articles) {
			Date date = ((Article)object).getDate();
			variableCalendar.setTime(date);
			if(rightNow.get(Calendar.YEAR) == variableCalendar.get(Calendar.YEAR) &&
					   rightNow.get(Calendar.MONTH) == variableCalendar.get(Calendar.MONTH)) {
				filteredeList.add(object);
					}
		}
		
		return filteredeList;
		
	}

	public List<Article> getMonthsAndYears() {
		return getHibernateTemplate().find("select distinct a.date from Article a where a.status=1");
	}

	public byte[] getArticleThumbnail(Long id) {
		Criteria criteria = getSession().createCriteria(Article.class);
		criteria.add(Expression.eq("id",id));
		criteria.setCacheable(true);
		List articles = criteria.list();
		return ((Article)articles.get(0)).getThumbnail();
	}

	public List<Article> getFeaturedArticles() {
		Criteria criteria = getSession().createCriteria(Article.class);
		criteria.setCacheable(true);
		criteria.add(Expression.eq("featured", new Byte("1")));
		criteria.addOrder(Order.desc("date"));
		List articles = criteria.list();
		return articles;
	}
}
